package cn.jiyun.controller;


import cn.jiyun.common.config.Result;
import cn.jiyun.entity.XmCouriers;
import cn.jiyun.entity.XmDistributionstations;
import cn.jiyun.mapper.XmDistributionstationsMapper;
import cn.jiyun.service.IXmCouriersService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-11-28
 */
@RestController
@RequestMapping("/xm-client/xm-couriers")
public class XmCouriersController {

    @Autowired
    private IXmCouriersService xmCouriersService;

//    查询配送员
    @RequestMapping("findAll")
    public Result findAll(Integer current,Integer pageSize,@RequestBody XmCouriers xmCouriers){
        Page<XmCouriers> xmCouriersPage=xmCouriersService.findAll(current,pageSize,xmCouriers);
        return Result.ok(xmCouriersPage);
    }

    @RequestMapping("/findByid/{id}")
    public XmCouriers findByid(@PathVariable Integer id){
        return xmCouriersService.getById(id);
    }


//    修改配送员的状态
    @RequestMapping("lockk")
    public Result lockk(@RequestBody XmCouriers xmCouriers){
        xmCouriersService.updateById(xmCouriers);
        return Result.ok();
    }


//    解绑/绑定站点
    @RequestMapping("jb")
    public Result jb(@RequestBody XmCouriers xmCouriers){
        System.out.println(xmCouriers);
        xmCouriersService.updateById(xmCouriers);
        return Result.ok();
    }


//    统计本周招募的配送员数量
    @RequestMapping("findAllCourier")
    public Result findAllCourier(){
        Integer integer = xmCouriersService.findAllCourier();
        return Result.ok(integer);
    }


//    统计昨天招募的配送员数量
    @RequestMapping("findAllCourier2")
    public Result findAllCourier2(){
        Integer integer = xmCouriersService.findAllCourier2();
        return Result.ok(integer);
    }

    @Autowired
    private XmDistributionstationsMapper xmDistributionstationsMapper;

//    把对应配送站的配送员减去
    @RequestMapping("updateCourier")
    public Result updateCourier(@RequestBody XmCouriers xmCouriers){
        QueryWrapper<XmDistributionstations> xmDistributionstationsQueryWrapper = new QueryWrapper<>();
        xmDistributionstationsQueryWrapper.eq("id",xmCouriers.getDid());
        XmDistributionstations xmDistributionstations = xmDistributionstationsMapper.selectOne(xmDistributionstationsQueryWrapper);
        xmDistributionstations.setCount(xmDistributionstations.getCount()-1);
        xmDistributionstationsMapper.updateById(xmDistributionstations);
        return Result.ok();
    }



    //    把对应配送站的配送员加上
    @RequestMapping("updateCourier2")
    public Result updateCourier2(@RequestBody XmCouriers xmCouriers){
        QueryWrapper<XmDistributionstations> xmDistributionstationsQueryWrapper = new QueryWrapper<>();
        xmDistributionstationsQueryWrapper.eq("id",xmCouriers.getDid());
        XmDistributionstations xmDistributionstations = xmDistributionstationsMapper.selectOne(xmDistributionstationsQueryWrapper);
        xmDistributionstations.setCount(xmDistributionstations.getCount()+1);
        xmDistributionstationsMapper.updateById(xmDistributionstations);
        return Result.ok();
    }
}
